package com.hrp.service;

import com.hrp.common.ChinesePinyinUtil;
import com.hrp.domain.Combox;
import com.hrp.domain.Jxkhzb;
import com.hrp.domain.Jxzblb;
import com.hrp.domain.Tree;
import com.hrp.hrpdao.JxkhzbDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/9 0009.
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public class JxkhzbService {


    @Autowired
    private JxkhzbDao jxkhzbDao;

    public List<Jxkhzb> getKhzbListByZblb(Integer zblb){

        List<Integer> strList = new ArrayList<>();
        if("-1".equals(zblb.toString())){
            List<Jxzblb> zblbList =  jxkhzbDao.getAllZblb();
            for(Jxzblb jxzblb : zblbList){
                strList.add(jxzblb.getId());
            }
        }else{
            strList.add(zblb);
        }
        return jxkhzbDao.getKhzbListByZblb(strList);
    }

    public List<Combox> getAllZblb(){
        List<Jxzblb> list = jxkhzbDao.getAllZblb();
        List<Combox> combList = new ArrayList<>();
        Combox combOne = new Combox();
        combList.add(combOne);
        combOne.setId("-1");
        combOne.setText("全部");
        combOne.setSelected(true);
        for(Jxzblb jxzblb:list){
            Combox comb = new Combox();
            comb.setId(jxzblb.getId().toString());
            comb.setText(jxzblb.getName());
            combList.add(comb);
        }
        return combList;
    }


    public List<Tree> getKhzbTreeListByZblb(Integer zblb){

        List<Tree> resTreeList = new ArrayList<>();

        Tree resTree = new Tree();
        resTree.setId("-1");
        if(zblb.toString().equals("1")){
            resTree.setText("考核指标");
        }else if(zblb.toString().equals("2")){
            resTree.setText("常量系数");
        }
        List<Integer> strList = new ArrayList<>();
        strList.add(zblb);

        List<Jxkhzb> khzbList = jxkhzbDao.getKhzbListByZblb(strList);
        List<Tree> cTreeList = new ArrayList<>();
        Tree cTree = null;
        for(Jxkhzb khzb:khzbList){
            cTree = new Tree();
            cTree.setId(khzb.getId().toString());
            cTree.setText(khzb.getZbmc());
            cTreeList.add(cTree);
        }
        resTree.setChildren(cTreeList);

        resTreeList.add(resTree);
        return resTreeList;
    }

    public List<Jxkhzb> getZbListByZqidAndJxksdm(String jxksdm,Integer zqid){
        return jxkhzbDao.getZbListByZqidAndJxksdm(jxksdm,zqid);
    }

    public void saveZbValueByJxksdmAndZqidAndZbid(Double value,String jxksdm,String zbdm,Integer zqid){
        jxkhzbDao.saveZbValueByJxksdmAndZqidAndZbid(value,jxksdm,zbdm,zqid);
    }

    public String getMaxZbid(){
        return jxkhzbDao.getMaxZbid();
    }

    public void saveKhzbData(Jxkhzb jxkhzb) {

        Jxkhzb jxkhzbbak = jxkhzbDao.getJxkhzbById(jxkhzb.getId());

        String cloumName = ChinesePinyinUtil.getPinYinHeadChar(jxkhzb.getZbmc());
        jxkhzb.setCloumName(cloumName);
        if(jxkhzbbak==null){
            jxkhzbDao.saveKhzbData(jxkhzb);
        }else{
            jxkhzbDao.updateJxkhzb(jxkhzb);
        }
    }



}
